<?php
    session_start();
    $currentUser = isset($_SESSION["user"])?$_SESSION["user"]:null;
    if(!$currentUser){
        echo "未登录";
        die(0);
    }

    //1
    //INPUT
    //查询条件 title,author...
    //$pageIndex当前多少页
    //$pageSize每页显示多少
    
    //1 $where 查询条件变成sql里面的$where
    //2 pageIndex/pageSize limit
    //3 通过1where limit ->$rows
    //4 通过1where ->$total
  
    //OUTPUT + INPUT
    //$rows 行数据
    //$total 总共有多少数据 业务层
    //-------------------------------------------------
    //OUTPUT + INPUT -> view 显示层

    //$rows
    //$total
    //一页三行

    $pageSize = 3;
    $pageIndex = "";
    $pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:null;
    if($pageIndex <= 0) $pageIndex = 1;

    $queryTitle = isset($_GET["queryTitle"])?$_GET["title"]:null;
    $queryAuthor = isset($_GET["queryAuthor"])?$_GET["author"]:null;
    $queryMinTime = isset($_GET["minTime"])?$_GET["minTime"]:null;
    $queryMaxTime = isset($_GET["maxTime"])?$_GET["maxTime"]:null;
    //INPUt数据 
    //========================================================================

    //2 
    //根据查询条件获取到sql中的where字句
    $where = "";
    if($queryTitle) {
        $where = " `Title` like ('%$queryTitle%')";
    }
    if($queryAuthor) {
        if($where) $where .= "And `AuthorName` like ('%$queryAuthor%')";
        else $where .= "`AuthorName` like ('%$queryAuthor%')";
    }

    if($queryMinTime){
        if($where) $where .= "And";
        $where .= "createTime >= '$queryMinTime'";
    }

    if($queryMaxTime){
        if($where) $where .= "And";
        $where .= "createTime <= '$queryMaxTime'";
    }


    if($where) $where = "WHERE $where";

    // 通过pageIndex/pageSize 构造limit limit ship，take-count

    $starRow = ($pageIndex -1)*$pageSize;

    //获取到$rows
    $sql = "select * from `article` $where order by `createTime` desc limit $starRow,$pageSize";
    $conn =  new mysqli("localhost","root","","myblog");
    $rs = $conn->query($sql);
    if($conn->error) die($conn->error);
    $rows = [];
    while($row = $rs ->fetch_assoc()){
        $rows[] = $row;
    }

    $rs->close();

    $totalSql = "select count(Id) as total from `article` $where";
    $rs = $conn->query($totalSql);
    $total = $rs->fetch_assoc()['total'];

    $pageCount = ceil($total/$pageSize);

    function makePageUrl($pageIndex){
        $url = "article_list.php?";
        foreach($_GET as $k =>$v){
            if($k==="pageIndex"){
                continue;
            }
            $url .=$k."=".$v."&"; 
        }
        $url .="pageIndex=".$pageIndex;
        return $url;
    }

?>

<html>
    <head>
        <title>文章列表</title>      
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

        <link href = "article_list_C.css" type = "text/css" rel = "Stylesheet"/>
    </head>   

    <body>
        <div>
            <span>当前用户:<?=$currentUser["Username"]?></span>
            <a href="logout.php">退出</a>
        <div>
        <h2>文章列表</h2>
        <a href="article_add.php" class = "btn">新添文章</a>
        <form action ="article_list.php" method = "get">
            <span>
                <label>标题</label>
                <input type = "text"  value="<?=$queryTitle?>" name = "title" />
            </span>
            <span>
                <label>作者</label>
                <input type = "text"  value="<?=$queryAuthor?>" name = "author" />
            </span>
            <span>
                <label>时间</label>
                <input type = "text"  value="<?=$queryMinTime?>" name = "minTime" />-
                <input type = "text"  value="<?=$queryMaxTime?>" name = "maxTime" />
            </span>
                <input type = "submit" value = "搜索"   />
        </form>
        <table>
            <thead>
                <tr>
                    <tH>
                        <div class = "title">标题</div>
                    </th>
                    <th>
                        作者
                    </th>
                    <th>
                        时间
                    </th>
                    <th>
                        操作
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php if($rows){?>
                    <?php foreach($rows as $row){ ?>
                    <tr>
                        <td><?=$row["Title"]?></td>
                        <td><?php echo $row["AuthorName"]?></td>
                        <td><?php echo $row["CreateTime"]?></td>
                        <td>
                            详情
                            修改
                        </td>
                    </tr>
                    <?php }?>
                <?php }else{?>
                    <td colspan = "4">
                        无数据
                    </td>
                <?php }?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan = "4">
                        < 共<?=$total?>条记录，共<?=$pageCount?>页，当前<?=$pageIndex?>页 >

                        <?php for($i=1;$i<=$pageCount;$i++) {?>
                            <a class = "<?=$pageIndex==$i?'currentPageNo':''?>" 
                            href ="<?=makePageUrl($i)?>"><?=$i?></a>
                        <?php } ?>
                        
                    </td>
                </tr>
            </tfoot>
        </table>
    </body>
</html>